package tools;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;

/**
 * handy functions to manipulate SWT images.
 * @author Nicolas Yzet <nicolas.yzet@gmail.com>
 *
 */
public class ImageUtils {

	/**
	 * scale an image to the given max height/width
	 * but keep the initial ratio.
	 * @param sourceImg base image
	 * @param maxHeight maximum height for the returned image
	 * @param maxWidth maximum width for the returned image
	 * @return the scaled image data
	 */
	public static ImageData scaleImage(Image sourceImg, int maxHeight, int maxWidth)
	{
		double minimizer = Math.min((double) maxWidth / sourceImg.getImageData().width,
				(double) maxHeight / sourceImg.getImageData().height);
		int imgWidth = (int)(sourceImg.getImageData().width*minimizer);
		int imgHeight = (int)(sourceImg.getImageData().height*minimizer);
		return sourceImg.getImageData().scaledTo(imgWidth, imgHeight);
	}
}
